//
//  ChangePasswordViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/9/1.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "NSString+Extension.h"
#import "IGRestPassWordApi.h"
#import "SettingsViewController.h"
#import "AppDelegate.h"

@interface ChangePasswordViewController ()<UITextFieldDelegate>{
    NSString *oldPsw;
    NSString *newPsw;
}
@property (weak, nonatomic) IBOutlet UITextField *originalPasswordTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *passwordNew;
@property (weak, nonatomic) IBOutlet UITextField *confirmPasswordTextField;
@property (strong, nonatomic) IBOutlet UIButton *confrimBtn;
@property (nonatomic, strong) IGRestPassWordApi *resetPassWordApi;

@end

@implementation ChangePasswordViewController
#pragma mart -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.title = @"修改密码";
    self.title = NSLocalizedString(@"Change Password", nil);

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
    // Do any additional setup after loading the view.
    self.originalPasswordTextFeild.keyboardType = UIKeyboardTypeAlphabet;
    self.passwordNew.keyboardType = UIKeyboardTypeAlphabet;
    self.confirmPasswordTextField.keyboardType = UIKeyboardTypeAlphabet;
    self.confrimBtn.layer.cornerRadius=20.f;
    [self.confrimBtn addTarget:self action:@selector(confirmButton:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -修改密码点击下一步
-(void)confirmButton:(UIButton *)btn{
    [self.view endEditing:YES];
    if (self.originalPasswordTextFeild.text.length == 0&&!self.originalPasswordTextFeild.text) {
//        [self presentMessageTips:@"请输入原始密码"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Original Password", nil)];
        return;
    }
    if (self.passwordNew.text.length == 0){
        [self presentMessageTips:NSLocalizedString(@"Please Input New Password", nil)];
        
        return;
    }
    if (self.passwordNew.text.length < 6) {
//        [self presentMessageTips:@"输入6-16位数字或字母"];
        [self presentMessageTips:NSLocalizedString(@"Please Input 6-16 Digits or Characters", nil)];

        return;
    }
    
    if (self.passwordNew.text.length > 16)
    {
//        [self presentMessageTips:@"密码格式不正确"];
        [self presentMessageTips:NSLocalizedString(@"Wrong Password Format", nil)];

        return;
    }
    if (self.confirmPasswordTextField.text.length==0) {
      [self presentMessageTips:NSLocalizedString(@"Please Input New Password Again", nil)];
        return;
    }
    
    if (![self.confirmPasswordTextField.text isEqualToString:self.passwordNew.text])
    {
        [self presentMessageTips:NSLocalizedString(@"Password not match,please confirm", nil)];

        return;
    }
    if (![self.passwordNew.text isPassword]) {
//        [self presentMessageTips:@"密码格式不正确"];
        [self presentMessageTips:NSLocalizedString(@"Wrong Password Format", nil)];

        return;
    }
    oldPsw=self.originalPasswordTextFeild.text;
    newPsw=self.passwordNew.text;
    
    //    oldPsw=[NSString md5_32Bit:oldPsw];
    //    newPsw=[NSString md5_32Bit:newPsw];
    
    self.resetPassWordApi = [IGRestPassWordApi new];
    self.resetPassWordApi.oldPw = oldPsw;
    self.resetPassWordApi.Pw = newPsw;
    [self.resetPassWordApi startWithDecodeClass:[IGRestPassWordResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
//            [self presentMessageTips:@"密码修改成功"];
            [self presentMessageTips:NSLocalizedString(@"Password Changed Successfully", nil)];

            [Tool performBlock:^{
                [kAPPDELEGATE configLoginVC];
            } afterDelay:1];
        }
    }];
    
}

- (void)userDidTakeScreenshot {
//    [UIAlertView bk_showAlertViewWithTitle:@"" message:@"您的手机正在被截屏\n注意保护密码安全" cancelButtonTitle:@"确认" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
//    }];
    [UIAlertView bk_showAlertViewWithTitle:@"" message:NSLocalizedString(@"You mobile screen is captured.\n Please pay attention to password security.", nil) cancelButtonTitle:NSLocalizedString(@"Confirm",nil) otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
    }];
}

@end
